跳到主要内容

【教程】基于 Ansible 在 Ubuntu 部署控制面 LB

这是一篇开发文档, 面向开发人员以及 AI.

本文以 Ubuntu 为例,使用 Ansible 部署单节点 HAProxy,仅承担 RKE2/Kubernetes 控制面入口的 L4 TCP 负载.

适用范围

  • 只承载控制面入口(6443/9345),不负责业务流量或 Ingress.
  • 单节点 LB,不具备高可用.
  • 公有云无托管 L4 LB,或成本限制时可用.

前置条件

  • 1 台 Ubuntu 服务器作为 LB 节点.
  • LB 与控制面节点同地域/同内网,延迟稳定优先.
  • 开放 6443/TCP9345/TCP 到控制面节点.

初始化 Ansible 项目

初始化仓库

首先创建文件夹,假设项目名为 lb-ansible

yun@yun ~/V/a/y/p/ansible (main)> mkdir lb-ansible
yun@yun ~/V/a/y/p/ansible (main)> ls
lb-ansible/

进入项目仓库,初始化 git, 创建 github 仓库:

cd lb-ansible
git init
echo "# lb-ansible" > README.md
git add .
git commit -m "chore: initial commit"
gh repo create lb-ansible --private --source=. --remote=origin --push

下面这段代码是可选的,用于将新建的代码仓库声明为子仓库:

cd ..
rm -rf lb-ansible/

git submodule add https://github.com/<用户名或组织>/lb-ansible.git ./lb-ansible

规划目录结构

接下来划分项目结构:

mkdir -p inventories/prod \
group_vars \
playbooks \
templates

使用 micro 创建并编辑文件:

micro ansible.cfg \
inventories/prod/hosts.yml \
group_vars/lb.yml \
playbooks/lb.yml \
templates/haproxy.cfg.j2

目录结构如下:

lb-ansible/
├── ansible.cfg
├── inventories/
│ └── prod/
│ └── hosts.yml
├── group_vars/
│ └── lb.yml
├── playbooks/
│ └── lb.yml
└── templates/
└── haproxy.cfg.j2

配置 Ansible

micro ansible.cfg :

[defaults]
inventory = inventories/prod/hosts.yml
remote_user = root
host_key_checking = False
timeout = 30

编写 inventory

micro inventories/prod/hosts.yml :

all:
children:
lb:
hosts:
lb-1:

可直接使用 SSH Config 中的别名,无需再写 ansible_host.

编写变量

micro group_vars/lb.yml :

haproxy_bind_ip: "0.0.0.0"
haproxy_api_port: 6443
haproxy_reg_port: 9345

rke2_api_backends:
- name: rke2-server1
host: 10.0.0.11
- name: rke2-server2
host: 10.0.0.12
- name: rke2-server3
host: 10.0.0.13

编写模板

micro templates/haproxy.cfg.j2 :

global
log /dev/log local0
maxconn 4096

defaults
mode tcp
timeout connect 5s
timeout client 30s
timeout server 30s

frontend rke2_api
bind {{ haproxy_bind_ip }}:{{ haproxy_api_port }}
default_backend rke2_api

backend rke2_api
option tcp-check
default-server inter 2s fall 3 rise 2
{% for backend in rke2_api_backends %}
server {{ backend.name }} {{ backend.host }}:{{ haproxy_api_port }} check
{% endfor %}

frontend rke2_reg
bind {{ haproxy_bind_ip }}:{{ haproxy_reg_port }}
default_backend rke2_reg

backend rke2_reg
option tcp-check
default-server inter 2s fall 3 rise 2
{% for backend in rke2_api_backends %}
server {{ backend.name }} {{ backend.host }}:{{ haproxy_reg_port }} check
{% endfor %}

编写 Playbook

micro playbooks/lb.yml :

- name: Deploy control plane LB
hosts: lb
become: true
tasks:
- name: Install haproxy
ansible.builtin.apt:
name: haproxy
state: present
update_cache: true

- name: Deploy haproxy config
ansible.builtin.template:
src: templates/haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg
owner: root
group: root
mode: "0644"
notify: Restart haproxy

- name: Enable haproxy
ansible.builtin.service:
name: haproxy
enabled: true
state: started

handlers:
- name: Restart haproxy
ansible.builtin.service:
name: haproxy
state: restarted

执行与验证

执行部署:

ansible-playbook playbooks/lb.yml

检查端口:

ss -lntp | grep -E ":6443|:9345"

控制面入口使用 LB 节点 IP 或域名,在 RKE2 变量中将 rke2_api_ip 设为该地址.